package user;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

import cake.User;
import cake.UserDao;
import cake.UserDaoImpl;


@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String data=req.getParameter("data");

        String name = req.getParameter("name");
        String pwd = req.getParameter("password");
        String email = req.getParameter("email");

        //实例化一个对象，组装属性
        User user = new User();
        user.setName(name);
        user.setPwd(pwd);
        user.setEmail(email);

        UserDao ud = new UserDaoImpl();
        if (ud.register(user)){
            System.out.println("用户" + name + "注册成功");
            req.setAttribute("msg","用户" + name + "注册成功");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }else {
            resp.sendRedirect("error.jsp");
        }
    }
}
